<?php 

class Config {
    private $data;
        
    public function __construct($array = array()) {
    	$this->data = array();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->data[$key] = new self($value);
            } else {
                $this->data[$key] = $value;
            }
        }
    }
    
    public function get($name, $default = null) {
        $result = $default;
        if (array_key_exists($name, $this->data)) {
            $result = $this->data[$name];
        }
        return $result;
    }

    public function __get($name) {
		return $this->get($name);
    }

    public function __set($name, $value) {
		if (is_array($value)) {
			$this->data[$name] = new self($value);
		} else {
			$this->data[$name] = $value;
		}
    }

    public function __clone() {
      	$array = array();
      	foreach ($this->data as $key => $value) {
          	if ($value instanceof Config) {
              	$array[$key] = clone $value;
          	} else {
              	$array[$key] = $value;
          	}
      	}
      	$this->data = $array;
    }

    public function to_array() {
        $array = array();
        $data = $this->data;
        foreach ($data as $key => $value) {
            if ($value instanceof Config) {
                $array[$key] = $value->to_array();
            } else {
                $array[$key] = $value;
            }
        }
        return $array;
    }

    public function __isset($name) {
        return isset($this->data[$name]);
    }
}

?>